package com.adkfp.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adkfp.utils.JsonUtil;
import com.adkfp.utils.LayuiUtil;
import com.adkfp.bean.ADKNetGroupBean;
import com.adkfp.service.IADKNetGroupService;

/**
 * class name:ADKNetGroupAction <BR>
 * class description: ADKNetGroup表的增删改查 <BR>
 * Remark: <BR>
 * @version 1.00 2019年01月03日
 * @author JACK)jackwei
 */
@Controller
@RequestMapping("aDKNetGroup")
public class ADKNetGroupAction {
	@Resource(name="aDKNetGroupService")
	private IADKNetGroupService aDKNetGroupService;

  /**
 	* Method name: selectAll <BR>
 	* Description: 查询所有记录 <BR>
 	* Remark: <BR>
 	* @param bean
 	* @return  Object<BR>
 	*/
	@RequestMapping("selectAll")
	@ResponseBody
	public Object selectAll(ADKNetGroupBean bean) {
		List<ADKNetGroupBean> list = new ArrayList<>();
		list = aDKNetGroupService.findByCondition(bean);
		long count = aDKNetGroupService.findByConditionCount(bean);
		return LayuiUtil.data(count, list);
	}
	
	/**
	 * Method name: index <BR>
	 * Description: 转发到组织机构查询页面-黄嘉颖 <BR>
	 * Remark: <BR>
	 * @return  String<BR>
	 */

	
	@RequestMapping("index1")
	public String index1() {
		return "aDKNetGroup/addADKNetGroup";
	}
	
	//跳转树页面
	@RequestMapping("tree")
	public String tree() {
		return "aDKNetGroup/NetGroupTree";
	}

	
	//跳转树页面Demo
		@RequestMapping("treeDemo")
		public String treeDemo() {
			return "aDKNetGroup/TreeDemo";
		}
	/**
	 * Method name: options <BR>
	 * Description: ADKNetGroup表的增删改 <BR>
	 * Remark: <BR>
	 * @param request
	 * @param response
	 * @return  String<BR>
	 */
	@RequestMapping("options")
	@ResponseBody
	public String options(@RequestParam("op")String op, @RequestParam("data")String data) {
		String f = "";
		
		switch (op) {
			case "add":
				if(data!=null&&!data.equals("")) {
					List<ADKNetGroupBean> list = JsonUtil.getList4Json(data, ADKNetGroupBean.class);
					for (ADKNetGroupBean bean : list) {
						aDKNetGroupService.insert(bean);
					}
					f = "增加成功";
				}
				break;
			case "del":
				if(data!=null&&!data.equals("")) {
					List<ADKNetGroupBean> list = JsonUtil.getList4Json(data, ADKNetGroupBean.class);
					for (ADKNetGroupBean bean : list) {
						aDKNetGroupService.delete(bean.getNetgroupid());
					}
					f = "删除成功";
				}
				break;
			case "upd":
				if(data!=null&&!data.equals("")) {
					List<ADKNetGroupBean> list = JsonUtil.getList4Json(data, ADKNetGroupBean.class);
					for (ADKNetGroupBean bean : list) {
						aDKNetGroupService.update(bean);
					}
					f = "更新成功";
				}
				break;
			default:
				break;
		}
		return f;
	}
	
	
	
	/**
	 * Method name: insertNetGroup <BR>
	 * Description: 增加/修改组织机构-黄嘉颖 <BR>
	 * Remark: <BR>
	 * @param teachingLogBean  void<BR>
	 */
	@RequestMapping("insertNetGroup")
	@ResponseBody
	public void insertNetGroup(ADKNetGroupBean netgroup) {
		//System.out.println("========"+netgroup.getNetgroupid());
		if(netgroup.getNetgroupid()!=null&&netgroup.getNetgroupid()!=0) {
			aDKNetGroupService.update(netgroup);
		}else {
		    aDKNetGroupService.insert(netgroup);
		}
	}
	
	//用于修改页面(查找id)
	@RequestMapping("findId")
	public String findId(HttpServletRequest request,String id,ADKNetGroupBean netGroup) {
		//System.out.println("id------"+id);
		if(id != null && !id.equals("")) {
		     netGroup=aDKNetGroupService.findById(Integer.parseInt(id));
		}
		request.setAttribute("zuzhi",netGroup);
		return "aDKNetGroup/addADKNetGroup";
	}
	
	//删除组织机构
	@RequestMapping("deleteNetGroup")
	@ResponseBody
	public String deleteNetGroup(String id) {
		System.out.println("----id----"+id);
		if(id!=null&&!id.equals("")&&!id.equals(1)) {
		aDKNetGroupService.delete(Integer.parseInt(id));
		return "SUCCESS";
	  }else {
		return "ERROR";
		}
	}
	
	//查找组织机构名称
	@RequestMapping("selectNetGroupName")
	@ResponseBody
	public Object selectrealName(ADKNetGroupBean netGroup) {
		List<ADKNetGroupBean> list = new ArrayList<>();
		list = aDKNetGroupService.selectNetGroupName();
		return list;
	}
	
	
	//验证名称唯一(FMY)
	@RequestMapping("checkSname")
	@ResponseBody
	public Object checkSname(ADKNetGroupBean bean,HttpServletRequest request) {
		String netgroupname=request.getParameter("netgroupname");
		//System.err.println("s哈哈哈+"+netgroupname);
		List<ADKNetGroupBean> list = new ArrayList<>();
		long count = aDKNetGroupService.checkSname(bean);
		//System.err.println("基督教的+"+count);
		//return LayuiUtil.data(count,list);
		return count;
	}
}